# League of ChangAn/champions.py
from abc import ABC, abstractmethod
import pygame
import os

class Champion(ABC):
    def __init__(self, name, health, skill, background, image):
        self.name = name
        self.health = health
        self.max_health = health
        self.skill = skill
        self.background = background
        self.image_path = image  
        self.armor = 0
        self.skill_cooldown = 0
        # 正确调用load_character_image方法并赋值给self.image
        self.image = self.load_character_image()
    
    def load_character_image(self):
        try:
            if self.image_path and os.path.exists(self.image_path):
                image = pygame.image.load(self.image_path).convert_alpha()
                return pygame.transform.scale(image, (100, 150))
            else:
                print(f"Warning: Character image {self.image_path} does not exist")
                # 从game模块中局部导入DEFAULT_CHARACTER_IMAGE
                from game import DEFAULT_CHARACTER_IMAGE
                return DEFAULT_CHARACTER_IMAGE
        except Exception as e:
            print(f"Error loading character image: {e}")
            from game import DEFAULT_CHARACTER_IMAGE
            return DEFAULT_CHARACTER_IMAGE
    
    def draw_character(self, screen, x, y):
        screen.blit(self.image, (x, y))
        
        name_text = pygame.font.SysFont(["SimHei", "WenQuanYi Micro Hei", "Heiti TC"], 24).render(self.name, True, (255, 255, 255))
        screen.blit(name_text, (x + 10, y + 160))
        
        health_text = pygame.font.SysFont(["SimHei", "WenQuanYi Micro Hei", "Heiti TC"], 24).render(f"Health: {self.health}/{self.max_health}", True, (100, 255, 100))
        screen.blit(health_text, (x + 10, y + 190))
        
        if self.armor > 0:
            armor_text = pygame.font.SysFont(["SimHei", "WenQuanYi Micro Hei", "Heiti TC"], 24).render(f"Armor: {self.armor}", True, (100, 150, 255))
            screen.blit(armor_text, (x + 10, y + 220))
    
    def reduce_cooldown(self):
        if self.skill_cooldown > 0:
            self.skill_cooldown -= 1
    
    @abstractmethod
    def use_skill(self, player, opponent, target=None):
        """使用英雄技能的抽象方法"""
        pass
    
    def is_skill_ready(self):
        return self.skill_cooldown == 0


class GeneralLi(Champion):
    """李将军 - 对敌方英雄造成伤害"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        damage = self.skill["value"]
        opponent.health = max(0, opponent.health - damage)
        add_battle_log(f"{self.name} uses skill, dealing {damage} damage to {opponent.name}")
        self.skill_cooldown = 2  # 技能冷却2回合
        return True


class HealerWang(Champion):
    """王医师 - 治疗自己"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        heal_amount = self.skill["value"]
        self.health = min(self.max_health, self.health + heal_amount)
        add_battle_log(f"{self.name} uses skill, healing {heal_amount} health")
        self.skill_cooldown = 3  # 技能冷却3回合
        return True


class StrategistZhao(Champion):
    """赵军师 - 召唤护卫"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log, Card
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        if len(player.board) >= 7:
            add_battle_log("Board is full, cannot summon minion")
            return False
            
        minion_data = self.skill["minion"]
        minion_data["type"] = "minion"
        minion = Card(minion_data, player.name)
        minion.summoned_this_turn = True
        player.board.append(minion)
        add_battle_log(f"{self.name} uses skill, summoning {minion.name}")
        self.skill_cooldown = 4  # 技能冷却4回合
        return True


class LadyLin(Champion):
    """林女侠 - 对敌方随从造成伤害"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        if not target or target not in opponent.board or not target.is_alive():
            add_battle_log("Invalid target for Lady Lin's skill")
            return False
            
        damage = self.skill["value"]
        target.take_damage(damage)
        add_battle_log(f"{self.name} uses skill, dealing {damage} damage to {target.name}")
        self.skill_cooldown = 2  # 技能冷却2回合
        return True


class ChiefQian(Champion):
    """钱帮主 - 获得护甲"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        armor_gain = self.skill["value"]
        self.armor += armor_gain
        add_battle_log(f"{self.name} uses skill, gaining {armor_gain} armor")
        self.skill_cooldown = 2  # 技能冷却2回合
        return True


class MasterSun(Champion):
    """孙道长 - 召唤带有护盾的弟子"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log, Card
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        if len(player.board) >= 7:
            add_battle_log("Board is full, cannot summon minion")
            return False
            
        minion_data = self.skill["minion"]
        minion_data["type"] = "minion"
        minion = Card(minion_data, player.name)
        minion.summoned_this_turn = True
        player.board.append(minion)
        add_battle_log(f"{self.name} uses skill, summoning {minion.name}")
        self.skill_cooldown = 5  # 技能冷却5回合
        return True


class AlchemistZhou(Champion):
    """周药师 - 增加友方随从攻击力"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        if not target or target not in player.board or not target.is_alive():
            add_battle_log("Invalid target for Alchemist Zhou's skill")
            return False
            
        buff_amount = self.skill["value"]
        target.attack += buff_amount
        add_battle_log(f"{self.name} uses skill, {target.name} gains +{buff_amount} attack")
        self.skill_cooldown = 3  # 技能冷却3回合
        return True


class AssassinWu(Champion):
    """吴刺客 - 对敌方英雄造成伤害但自身也受伤害"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        damage_to_enemy = self.skill["value"]
        self_damage = 1
        
        opponent.health = max(0, opponent.health - damage_to_enemy)
        self.health = max(0, self.health - self_damage)
        
        add_battle_log(f"{self.name} uses skill, dealing {damage_to_enemy} damage to {opponent.name} but taking {self_damage} damage")
        self.skill_cooldown = 3  # 技能冷却3回合
        return True


class InstructorZheng(Champion):
    """郑教头 - 召唤学员"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log, Card
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        if len(player.board) >= 7:
            add_battle_log("Board is full, cannot summon minion")
            return False
            
        minion_data = self.skill["minion"]
        minion_data["type"] = "minion"
        minion = Card(minion_data, player.name)
        minion.summoned_this_turn = True
        player.board.append(minion)
        add_battle_log(f"{self.name} uses skill, summoning {minion.name}")
        self.skill_cooldown = 3  # 技能冷却3回合
        return True


class MerchantFeng(Champion):
    """冯掌柜 - 抽牌但损失生命值"""
    def use_skill(self, player, opponent, target=None):
        from game import add_battle_log
        if not self.is_skill_ready():
            add_battle_log(f"{self.name}'s skill is on cooldown ({self.skill_cooldown} turns remaining)")
            return False
            
        cards_to_draw = self.skill["value"]
        player.draw_card(cards_to_draw)
        self.health = max(0, self.health - 1)
        add_battle_log(f"{self.name} uses skill, draws {cards_to_draw} card but loses 1 health")
        self.skill_cooldown = 4  # 技能冷却4回合
        return True